﻿using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;

using Resn.WhuPub.BLL;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// StartPage.xaml 的交互逻辑
        /// </summary>
        public partial class StartPage : Page
        {
                private readonly GlobalManager _gm = GlobalManager.Instance;

                public StartPage()
                {
                        InitializeComponent();
                }

                private void Page_Loaded(object sender, RoutedEventArgs e)
                {
                }

                private void LoginButton_Click(object sender, RoutedEventArgs e)
                {
                        string name = NameTextBox.Text.Trim().ToLower();
                        string password = PasswordTextBox.Password.Trim().ToLower();
                        string md5password =
                                Encoding.Default.GetString(MD5.Create().ComputeHash(Encoding.Default.GetBytes(password)));

                        Employee employee = _gm.EmployeeEntityManager.Entitys.FirstOrDefault(i => i.Name == name);
                        if (employee == null)
                        {
                                MessageBox.Show("无此用户");
                                return;
                        }

                        if (employee.Password != md5password)
                        {
                                MessageBox.Show("密码错误，请重新输入密码");
                                return;
                        }

                        if (employee.State != EmployeeState.在职)
                        {
                                MessageBox.Show("账号锁定，请联系管理员");
                                return;
                        }

                        _gm.LoginEmployee = employee;
                        NavigationService.Navigate(new Uri("EmployeeBusinessPage.xaml", UriKind.Relative));
                }
        }
}